//
//  DLGroupListController.m
//  AllPass
//
//  Created by apple on 2021/11/2.
//

#import "DLGroupListController.h"
#import "ImageFactory.h"
#import <ACActionSheet/ACActionSheet.h>
#import "DLAddGroupVC.h"
#import "BXNavigationController.h"
#import "DLRecordListVC.h"
#import "DLAddPswVC.h"
@interface DLGroupListController ()
@property(nonatomic,retain)NSMutableArray * groupArr, * recordArr;
@property(nonatomic,retain)DatabaseDocument * db;
@end

@implementation DLGroupListController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor =UnderLineColor;
    self.rightBtnImage = Image(@"添加_首页");
    self.db = [[VicSingleObject getInstance] db];
    NSArray *arr = [self.db.filename componentsSeparatedByString:@"/"];
    if (arr.count > 0) {
        self.myTitle = arr.lastObject;
    }
    [self createUI];
    [self getAllData];
}
- (void)rightBtnClick{
    ACActionSheet *actionSheet = [[ACActionSheet alloc] initWithTitle:nil cancelButtonTitle:NSLocalizedString(@"cancel", nil) destructiveButtonTitle:nil otherButtonTitles:@[NSLocalizedString(@"Groups", nil),NSLocalizedString(@"Entries", nil)] actionSheetBlock:^(NSInteger buttonIndex) {
        if (buttonIndex == 0) {
            DLAddGroupVC * vc = [DLAddGroupVC new];
            vc.doneBlock = ^{
                [self.groupArr removeAllObjects];
                [self getGroupData];
                [self.myTab reloadData];
            };
            BXNavigationController * nav = [[BXNavigationController alloc]initWithRootViewController:vc];
            [self presentViewController:nav animated:YES completion:nil];
        }else if (buttonIndex == 1){
            DLAddPswVC * vc = [DLAddPswVC new];
            vc.group = self.db.kdbTree.root;
            vc.reloadBlock = ^{
                [self.recordArr removeAllObjects];
                [self getRecordData];
                [self.myTab reloadData];
            };
            [self.navigationController pushViewController:vc animated:YES];
        }
    }];
    [actionSheet show];
}
-(void)getAllData{
    [self getGroupData];
    [self getRecordData];
    [self.myTab reloadData];
}
-(void)getRecordData{
    [self.recordArr addObjectsFromArray:self.db.kdbTree.root.entries];
}
-(void)getGroupData{
    [self.groupArr addObjectsFromArray:self.db.kdbTree.root.groups];
}
#pragma mark 代理方法
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if (self.groupArr.count > 0 && self.recordArr.count > 0) {
        return 2;
    }else if(self.groupArr.count == 0 && self.recordArr.count == 0){
        return 0;
    }else{
        return 1;
    }
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return self.groupArr.count;
    }else{
        return self.recordArr.count;
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    NSString * name = nil;
    if (indexPath.section== 0) {
        if (indexPath.row < self.groupArr.count) {
            KdbGroup * g = self.groupArr[indexPath.row];
            name = g.name;
            cell.imageView.image = [[ImageFactory sharedInstance] imageForGroup:g];
        }
    }else{
        if (indexPath.row < self.recordArr.count) {
            KdbEntry * e = self.recordArr[indexPath.row];
            name = e.title;
            cell.imageView.image = [[ImageFactory sharedInstance] imageForEntry:e];
            
            NSString *detailText = @"";
            if (e.username.length > 0) {
                detailText = e.username;
            }
            if (e.url.length > 0) {
                if (detailText.length > 0) {
                    detailText = [NSString stringWithFormat:@"%@ @ %@", detailText, e.url];
                } else {
                    detailText = e.url;
                }
            }
            cell.detailTextLabel.text = detailText;
        }
    }
    cell.textLabel.text = name;
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if(section == 0){
        if (self.groupArr.count > 0) {
            return 40;
        }
    }else{
        if (self.recordArr.count > 0) {
            return 40;
        }
    }
    return 0.01;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if(section == 0){
        if (self.groupArr.count > 0) {
            return [self createHeadView:section];
        }
    }else{
        if (self.recordArr.count > 0) {
            return [self createHeadView:section];
        }
    }
    return nil;
}
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    return NSLocalizedString(@"Delete", nil);
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        [self.db.kdbTree.root removeGroup:self.groupArr[indexPath.row]];
        [self.groupArr removeObjectAtIndex:indexPath.row];
    }else{
        [self.db.kdbTree.root removeEntry:self.recordArr[indexPath.row]];
        [self.recordArr removeObjectAtIndex:indexPath.row];
    }
    [self.db save];
    [self.myTab reloadData];
}
-(UIView *)createHeadView:(NSInteger)section{
    UIView * v = [UIView new];
    v.backgroundColor = UnderLineColor;
    NSString * name = section == 0 ? NSLocalizedString(@"Groups", nil):NSLocalizedString(@"Entries", nil);
    UILabel * lab = [UILabel labelWithTitle:name color:TextColor fontSize:16];
    [v addSubview:lab];
    lab.sd_layout.leftSpaceToView(v, 10).centerYEqualToView(v).heightIs(18);
    [lab setSingleLineAutoResizeWithMaxWidth:300];
    return v;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    KdbGroup * group = self.groupArr[indexPath.row];
    DLRecordListVC * vc = [DLRecordListVC new];
    vc.group = group;
    [self.navigationController pushViewController:vc animated:YES];
}
-(void)createUI{
    self.myTab = [[UITableView alloc]initWithFrame:Frame(0, kTopHeight, SCREEN_WIDTH, SCREEN_HEIGHT - kTopHeight - kTabbarHeight) style:UITableViewStyleGrouped];
    self.myTab.delegate = self;
    self.myTab.dataSource = self;
    [self.view addSubview:self.myTab];
}
- (NSMutableArray *)groupArr{
    if (!_groupArr) {
        _groupArr = [NSMutableArray new];
    }
    return _groupArr;
}
- (NSMutableArray *)recordArr{
    if (!_recordArr) {
        _recordArr = [NSMutableArray new];
    }
    return _recordArr;
}
@end
